package src.test03;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DeliverCallback;
import src.util.ConnectionUtil;
import src.util.SleepUtils;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2023-08-21
 * Time: 22:03
 */
public class Consumeer02 {
    public static final String QUEUE_NAME = "queue_ack";
    public static void main(String[] args) throws Exception{
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();
        System.out.println("C2等待接受消息处理消息时间长");

        //采用手动应答
        boolean autoAck = false;

        DeliverCallback var3 = (consumerTag, message)->{
            //睡眠该线程1秒
            SleepUtils.sleep(5);
            System.out.println("接受到消息" + new String(message.getBody()));
            /**手动应答
             *  1. 消息的标记
             *  2. 是否批量应答
             */
            channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
        };
        CancelCallback var4 = consumerTag -> {
            System.out.println("消费被中断");
        };
        //设置消费者为不公平分发原则
        channel.basicQos(30);
        channel.basicConsume(QUEUE_NAME, autoAck, var3, var4);
    }
}
